<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/1/8
 * Time: 9:36
 */
namespace app\api\controller;
use think\Db;
class Music extends Base {
    //banner图
    public function banner(){
        $res =  Db::table('up_banner')->where('banner_part',4)->select();
        return json(['status' => 200, 'msg' => $res]);
    }

    //新歌速递
    public function newMusic(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['is_new'] = 1;
        $where['is_type'] = 1;
        $res = Db::table('up_music')->alias('a')
            ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
            ->field('a.*,umm.video_modular_name')
            ->where($where)
            ->orderRaw('rand()')
            ->limit($limit*$num,$num)
            ->select();
        //查询是否收藏
        foreach ($res as $key=>&$vo){
            $where1['user_id'] = $user_id;
            $where1['is_type'] = 2;
            $where1['f_id'] = $vo['music_id'];
            $info = Db::table('up_like')->where($where1)->find();
            if($info['like_id']!=''){
                $vo['is_like'] = 1;
            }else{
                $vo['is_like'] = 2;
            }
        }
        return json(['status' => 200, 'msg' => $res]);
    }
    //所有戏曲
    public function allMusic(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $where['is_type'] = 1;
        $res = Db::table('up_music')->alias('a')
            ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
            ->field('a.*,umm.video_modular_name')
            ->where($where)
            ->limit($limit*$num,$num)
            ->select();
        //查询是否收藏
        foreach ($res as $key=>&$vo){
            $where1['user_id'] = $user_id;
            $where1['is_type'] = 2;
            $where1['f_id'] = $vo['music_id'];
            $info = Db::table('up_like')->where($where1)->find();
            if($info['like_id']!=''){
                $vo['is_like'] = 1;
            }else{
                $vo['is_like'] = 2;
            }
        }
        return json(['status' => 200, 'msg' => $res]);
    }

    //音乐馆
    public function hall(){
        extract(input());
        $res = Db::table('up_sort')->order('sort desc')->select();
        foreach ($res as $key=>&$vo){
            if($vo['sort_name']=='新编锡歌'){
                $info = Db::table('up_music_modular')->where('is_xbxg',1)->find();
                $result = Db::table('up_music')->alias('a')
                    ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                    ->field('a.*,umm.video_modular_name')
                    ->where('music_f_id',$info['video_modular_id'])
                    ->where('is_type',1)
                    ->orderRaw('rand()')
                    ->limit(8)
                    ->select();
                $vo['xbxg'] = $result;
            }
            if($vo['sort_name']=='唱段'){
                $info = Db::table('up_music_modular')->where('is_cd',1)->find();
                $result = Db::table('up_music')->alias('a')
                    ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                    ->field('a.*,umm.video_modular_name')
                    ->where('music_f_id',$info['video_modular_id'])
                    ->where('is_type',1)
                    ->orderRaw('rand()')
                    ->limit(10)
                    ->select();
                $vo['cd'] = $result;
            }

            if($vo['sort_name']=='音乐伴奏'){
                $info = Db::table('up_music_modular')->where('is_yybz',1)->find();
                $result = Db::table('up_music')->alias('a')
                    ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                    ->field('a.*,umm.video_modular_name')
                    ->where('music_f_id',$info['video_modular_id'])
                    ->where('is_type',1)
                    ->orderRaw('rand()')
                    ->limit(10)
                    ->select();
                $vo['cd'] = $result;
            }
            if($vo['sort_name']=='我们都在听'){
                $result = Db::table('up_music')->alias('a')
                    ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                    ->field('a.*,umm.video_modular_name')
                    ->where('is_listen',1)
                    ->where('is_type',1)
                    ->orderRaw('rand()')
                    ->limit(10)
                    ->select();
                $vo['wmdzt'] = $result;
            }
            if($vo['sort_name']=='VIP专区'){
                $result = Db::table('up_music')->alias('a')
                    ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                    ->field('a.*,umm.video_modular_name')
                    ->where('is_type',1)
                    ->where('is_vip',2)
                    ->orderRaw('rand()')
                    ->limit(3)
                    ->select();
                //查询是否收藏
                foreach ($result as $key=>&$vo1){
                    $where1['user_id'] = $user_id;
                    $where1['is_type'] = 2;
                    $where1['f_id'] = $vo1['music_id'];
                    $info = Db::table('up_like')->where($where1)->find();
                    if($info['like_id']!=''){
                        $vo1['is_like'] = 1;
                    }else{
                        $vo1['is_like'] = 2;
                    }
                }
                $vo['vip'] = $result;
            }
        }
        return json(['status' => 200, 'msg' => $res]);
    }
    //更多
    public function moreHall(){
        $where = array();
        $page = input('param.page')?input('param.page'):1;
        $num = 12;
        $limit = $page-1;
        $year = input('param.year');//年份
        $school = input('param.school');//流派
        $modular = input('param.modular');//剧种
        $type = input('param.type');//1从低到高   2从高到低
        if($year!=''){
            $where['a.music_year'] = $year;
        }
        if($school!=''){
            $where['a.music_lp'] = $school;
        }
        if($modular!=''){
            $where['a.music_f_id'] = $modular;
        }
        if($type!=''){
            if($type==1){
                $order = 'a.music_num asc';
            }elseif($type==2){
                $order = 'a.music_num desc';
            }
        }else{
            $order = 'a.music_num desc';
        }

        $info = Db::table('up_sort')->where('sort_id',input('param.id'))->find();
        if($info['sort_name']=='新编锡歌'){
            $info1 = Db::table('up_music_modular')->where('is_xbxg',1)->find();
            $result = Db::table('up_music')->alias('a')
                ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                ->field('a.*,umm.video_modular_name')
                ->where('music_f_id',$info1['video_modular_id'])
                ->where($where)
                ->where('is_type',1)
                ->order($order)
                ->limit($limit*$num,$num)
                ->select();
        }elseif ($info['sort_name']=='唱段'){
            $info1 = Db::table('up_music_modular')->where('is_cd',1)->find();
            $result = Db::table('up_music')->alias('a')
                ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                ->field('a.*,umm.video_modular_name')
                ->where('music_f_id',$info1['video_modular_id'])
                ->where($where)
                ->where('is_type',1)
                ->order($order)
                ->limit($limit*$num,$num)
                ->select();
        } elseif ($info['sort_name']=='音乐伴奏'){
            $info1 = Db::table('up_music_modular')->where('is_yybz',1)->find();
            $result = Db::table('up_music')->alias('a')
                ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                ->field('a.*,umm.video_modular_name')
                ->where('music_f_id',$info1['video_modular_id'])
                ->where($where)
                ->where('is_type',1)
                ->order($order)
                ->limit($limit*$num,$num)
                ->select();
        }   elseif ($info['sort_name']=='我们都在听'){
            $result = Db::table('up_music')->alias('a')
                ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                ->field('a.*,umm.video_modular_name')
                ->where('is_listen',1)
                ->where($where)
                ->where('is_type',1)
                ->order($order)
                ->limit($limit*$num,$num)
                ->select();
        }   elseif ($info['sort_name']=='VIP专区'){
            $result = Db::table('up_music')->alias('a')
                ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                ->field('a.*,umm.video_modular_name')
                ->where('is_type',1)
                ->where($where)
                ->where('is_vip',2)
                ->limit($limit*$num,$num)
                ->select();
        }
        return json(['status' => 200, 'msg' => $result]);
    }
    //流派
    public function school(){
        $res = Db::table('up_school')->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //剧种
    public function modular(){
        $res = Db::table('up_music_modular')->field('video_modular_id,video_modular_name')->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //年份
    public function year(){
        $res = Db::table('up_music')->group('music_year')->field('music_year')->select();
        return json(['status' => 200, 'msg' => $res]);
    }

    //背景图片
    public function banner1(){
        $res =  Db::table('up_backdrop')->where('backdrop_status',4)->find();
        return json(['status' => 200, 'msg' => $res]);
    }
    public function banner2(){
        $res =  Db::table('up_backdrop')->where('backdrop_status',1)->find();
        return json(['status' => 200, 'msg' => $res]);
    }

    //每日特推
    public function vip(){
        extract(input());
        $res = Db::table('up_music')->alias('a')
            ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
            ->field('a.*,umm.video_modular_name')
            ->where('is_type',1)
            ->where('is_vip',2)
            ->orderRaw('rand()')
            ->limit(3)
            ->select();
        //查询是否收藏
        foreach ($res as $key=>&$vo){
            $where1['user_id'] = $user_id;
            $where1['is_type'] = 2;
            $where1['f_id'] = $vo['music_id'];
            $info = Db::table('up_like')->where($where1)->find();
            if($info['like_id']!=''){
                $vo['is_like'] = 1;
            }else{
                $vo['is_like'] = 2;
            }
        }
        return json(['status' => 200, 'msg' => $res]);
    }
    //时下正流行
    public function popular(){
        $res = Db::table('up_music')->alias('a')
            ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
            ->field('a.*,umm.video_modular_name')
            ->where('is_sxzlx',1)
            ->where('is_vip',2)
            ->where('is_type',1)
            ->orderRaw('rand()')
            ->limit(10)
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //更多时下正流行
    public function morePopular(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $year = input('param.year');//年份
        $school = input('param.school');//流派
        $modular = input('param.modular');//剧种
        $type = input('param.type');//1从低到高   2从高到低
        $where = array();
        if($year!=''){
            $where['a.music_year'] = $year;
        }
        if($school!=''){
            $where['a.music_lp'] = $school;
        }
        if($modular!=''){
            $where['a.music_f_id'] = $modular;
        }
        if($type!=''){
            if($type==1){
                $order = 'a.music_num asc';
            }elseif($type==2){
                $order = 'a.music_num desc';
            }
        }else{
            $order = 'a.music_num desc';
        }

        $res = Db::table('up_music')->alias('a')
            ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
            ->field('a.*,umm.video_modular_name')
            ->where('is_sxzlx',1)
            ->where('is_vip',2)
            ->where($where)
            ->order($order)
            ->limit($limit*$num,$num)
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }
    //详情
    public function detail(){
        extract(input());
        //todo
        $id = input('param.id');
        $res = Db::table('up_music')->alias('a')
            ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
            ->field('a.*,umm.video_modular_name')
            ->where('is_type',1)
            ->where('a.music_id',$id)
            ->find();
        $result = Db::table('up_music_chirden')->where('f_id',$res['music_id'])->select();
        $res['chirden'] = $result;
        $where['user_id'] = $user_id;
        $where['is_type'] = 2;
        $where['f_id'] = $res['music_id'];
        $info = Db::table('up_like')->where($where)->find();
        if($info['like_id']!=''){
            $res['is_like'] = 1;
        }else{
            $res['is_like'] = 2;
        }

        $where1['type'] = 2;
        $where1['f_id'] = $res['music_id'];
        $num = Db::table('up_comment')->where($where1)->count();
        $res['comment_num'] = $num;
        return json(['status' => 200, 'msg' => $res]);
    }
    //评论详情
    public function detailComment(){
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $id = input('param.id');
        $res = Db::table('up_comment')->alias('a')
             ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
             ->field('uuu.user_username,uuu.user_image,a.*')
             ->where('a.f_id',$id)
            ->where('type',2)
            ->limit($limit*$num,$num)
            ->select();
        return json(['status' => 200, 'msg' => $res]);
    }

    public function next(){
        $type = input('param.type');
        $id = input('param.id');
        if($type==1){
            $res = Db::table('up_music')->alias('a')
                ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                ->field('a.*,umm.video_modular_name')
                ->where('is_type',1)
                ->where('music_id','<',$id)
                ->order('music_id desc')
                ->find();
        }else if($type==2){
            $res = Db::table('up_music')->alias('a')
                ->join('up_music_modular umm','umm.video_modular_id = a.music_f_id','left')
                ->field('a.*,umm.video_modular_name')
                ->where('is_type',1)
                ->where('music_id','>',$id)
                ->find();
        }
        if(count($res)>0){
            return json(['status' => 200, 'msg' => $res]);
        }else{
            return json(['status' => 100, 'msg' => '没有音乐了']);
        }

    }

    /***
     * 记录用户浏览视频的接口
     */
    public function record(){
        extract(input());
        $video_id = input('param.video_id');
        $where['user_id'] = $user_id;
        $where['video_id'] = $video_id;
        $res = Db::table('up_record_music')->where($where)->find();
        if($res['record_video_id']!=''){//查询到这个用户有这个视频的观看记录
            Db::table('up_record_music')->where('record_video_id',$res['record_video_id'])->setInc('video_num');
        }else{
            Db::table('up_record_music')->insert($where);
        }
        Db::table('up_music')->where('music_id',$video_id)->setInc('music_num');
        return json(['status' => 200, 'msg' => "成功"]);
    }

    /**
     * @return \think\response\Json
     * 退出观看历史
     */
    public function loginOut(){
        extract(input());
        $video_id = input('param.video_id');
        $where['user_id'] = $user_id;
        $where['video_id'] = $video_id;
        $res = Db::table('up_record_music')->where($where)->order('record_video_id desc')->find();
        $time =date('Y-m-d H:i:s',time());
        Db::table('up_record_music')->where('record_video_id',$res['record_video_id'])->setField('video_time',$time);
        return json(['status' => 200, 'msg' => $res]);

    }
}